package cn.kfu.controller;

import cn.kfu.model.User;
import cn.kfu.model.UserLoginLog;
import cn.kfu.service.UserLoginLogService;
import cn.kfu.service.UserService;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private UserLoginLogService userLoginLogService;

    @RequestMapping("register")
    public String register(
            @RequestParam("idNumber") String idNumber,
            @RequestParam("userName") String userName,
            @RequestParam("password") String password,
            Model model
    ) {
        User user = new User();
        user.setUserName(userName);
        user.setPassword(password);
        if (idNumber.contains("@")) {
            user.setEmail(idNumber);
        } else {
            user.setCellphone(idNumber);
        }
        userService.register(user);
        model.addAttribute("msg", "注册成功");
        return "login";
    }

    /*
    @RequestMapping("login")
    @ResponseBody
    public Map<String, String> login(@RequestBody User user) {
        Map<String, String> map = new HashMap<>();
        map.put("status", "error");
        try {
            String username = null;
            if (user.getCellphone() != null) {
                username = user.getCellphone();
            } else {
                username = user.getEmail();
            }
            Subject subject = SecurityUtils.getSubject();
            AuthenticationToken token = new UsernamePasswordToken(username, user.getPassword());
            subject.login(token);
//            登录成功
            map.put("status", "ok");
            map.put("msg", "登录成功");
        } catch (UnknownAccountException e) {
            e.printStackTrace();
            // 用户不存在
            map.put("msg", "用户不存在");
        } catch (IncorrectCredentialsException e) {
            e.printStackTrace();
            // 密码错误
            map.put("msg", "密码错误");
        } catch (Exception e) {
            e.printStackTrace();
            map.put("msg", "系统错误");
        }
        return map;
    }

     */
    @RequestMapping("login")
    public String login(@RequestParam("username") String username, @RequestParam("password") String password, Model model, HttpServletRequest request) {
        try {
            Subject subject = SecurityUtils.getSubject();
            AuthenticationToken token = new UsernamePasswordToken(username, password);
            subject.login(token);

            UserLoginLog userLoginLog = new UserLoginLog();
            userLoginLog.setLoginTime(new Date());
            userLoginLog.setLoginIp(request.getRemoteAddr());
            userLoginLog.setUser(userService.findUserByUsername(username));
            userLoginLogService.save(userLoginLog);

            List<UserLoginLog> list = userLoginLogService.findAll();
            model.addAttribute("list", list);
//            登录成功
            return "index";
        } catch (UnknownAccountException e) {
            e.printStackTrace();
            model.addAttribute("msg", "用户不存在");
            return "login";
        } catch (IncorrectCredentialsException e) {
            e.printStackTrace();
            model.addAttribute("msg", "密码错误");
            return "login";
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("msg", "系统错误");
            return "login";
        }
    }
}
